package com.taotao.zerocopy;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.nio.channels.SocketChannel;

/**
 * @Description: 传统IO客户端；
 * @Author: TaoJF
 * @Date: Created in 2018/7/8 下午3:33
 */
public class OldIOClient {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 8088);

        String fileName = "/Users/taotao/Downloads/VMware Fusion Pro8.rar";
        InputStream inputStream = new FileInputStream(fileName);

        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());

        byte[] byteArray = new byte[4069];
        long readCount;
        long totalReadCount = 0;

        long startTime = System.currentTimeMillis();

        while ((readCount = inputStream.read(byteArray)) >= 0) {
            totalReadCount += readCount;
            dataOutputStream.write(byteArray);
        }

        System.out.printf("发送总字节数：%d，耗时：%dms%n", totalReadCount, System.currentTimeMillis() - startTime);
        dataOutputStream.close();
        inputStream.close();
        socket.close();
    }

}
